<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" ref="refbut">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus"  @click="handleAdd" v-hasPermi="['archive:yqZtlx:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table ref="table" v-loading="loading" :data="dataList" :height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="ztlxId"/>
      <el-table-column label="载体类型名称" prop="ztlxName" align="center" :show-overflow-tooltip="true"  />
      <el-table-column label="载体类型" prop="type" align="center" :show-overflow-tooltip="true"  />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button  plain type="success" @click="handleUpdate(scope.row)" v-hasPermi="['archive:yqZtlx:edit']">修改</el-button>
          <el-button  plain type="danger" @click="handleDelete(scope.row)" v-hasPermi="['archive:yqZtlx:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 载体添加及修改 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-form-item label="载体类型名称" prop="ztlxName">
            <el-input v-model="form.ztlxName" placeholder="请输入载体类型名称"></el-input>
          </el-form-item>
          <el-form-item label="载体类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择" clearable style="width: 100%">
              <el-option v-for="dict in dict.type.ZTLX" :key="dict.value" :label="dict.label"
                         :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import estimateTableHeight from "@/mixins/estimateTableHeight";
import {addType, getTableList, getTypeDetail, updateType, deleteType} from "@/api/archive/filesManage/carrierType";

export default {
  name: "index",
  mixins: [estimateTableHeight], // 混入方法
  archive_Dicts: ['ZTLX'],
  data() {
    return {
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dataList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      title: '',
      open: false,
      // 表单参数
      form: {
        ztlxName: undefined,
        type: undefined,
      },
      ztlxNameOption: [],
      // 表单校验
      rules: {
        ztlxName: [
          { required: true, message: '请输入载体类型名称', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请输入载体类型', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getTableList(this.queryParams).then(response=>{
        this.dataList = response.rows
        for (let i = 0; i < response.rows.length; i++) {
          for (let j = 0; j < this.dict.type.ZTLX.length; j++) {
            if(response.rows[i].type == this.dict.type.ZTLX[j].value) {
              this.dataList[i].type = this.dict.type.ZTLX[j].label
            }
          }
        }
        this.total = response.total
        this.loading = false
      })
    },
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ztlxId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    reset() {
      this.form = {
        ztlxName: undefined,
        type: undefined,
      }
    },
    // 新增载体类型
    handleAdd() {
      this.resetForm('form')
      this.reset()
      this.open = true
      this.title = '新增载体类型'
    },
    // 修改载体类型
    handleUpdate(row) {
      this.resetForm('form')
      this.reset()
      const id = row.ztlxId || this.ids
      getTypeDetail(id).then(response=>{
        this.form = response.data
        this.form.type = response.data.type.toString()
        this.title = '修改载体类型'
        this.open = true
      })
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if(valid) {
          if(this.form.ztlxId != undefined) {
            updateType(this.form).then(response=>{
              this.$modal.msgSuccess(response.msg)
              this.open = false
              this.getList()
            })
          } else {
            addType(this.form).then(response=>{
              this.$modal.msgSuccess(response.msg)
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    cancelForm() {
      this.open = false
    },
    // 删除载体类型
    handleDelete(row) {
      const id = row.ztlxId || this.ids
      this.$modal.confirm('是否确认删除选中载体名称为"'+ row.ztlxName +'"的数据项？').then(function () { }).then(() => {
        deleteType(id).then(res => {
          if (res.code == "200") {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          }
        })
      }).catch(() => { });
    }
  }
}
</script>

<style scoped>

</style>
